package com.pic.mgt.executer;

import org.springframework.dao.DataAccessException;

import com.pic.mgt.dao.IBusinessTargetActionDao;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.operation.MessageUnitOperation;
import com.pic.mgt.utils.LogUtils;

public abstract class MessageUnitExecuter extends UnitExecuter {
	private Message message;
	
	protected IBusinessTargetActionDao businessTargetActionDao; 
	
	protected MessageUnitOperation simpleMessageUnitOperation;
	
	@Override
	protected void executeUnitType() throws UnitTypeExecutionException {
		this.message = (Message)super.unit;
		
		// 預留
		
		try {
			// throws MessageTypeExecutionException
			executeMessageType();
		} catch (MessageTypeExecutionException e) {
			// ********************** Log **********************
			LogUtils.error(this, IgxLog.messageDetailsLog, e);
			// ********************** Log **********************
			
			throw new UnitTypeExecutionException(
					"Message '" + message.getUnitName() +
					"' execution failed in Message type process.", e);
		}
	}
	
	// (送多方刪除)
	/**
	 * 更新 *_business_target_action table 內的最後交易時間。
	 * @param message
	 */ /*
	protected void updateBusinessTargetActionTxTime(Message message) {
		// 更新資料 (table: *_business_target_action)
		this.businessTargetActionDao.updateLastTxTimeByFilename(
				new Timestamp(message.getTransactionTime().getTime()),
				message.getTransactionFilename());
	}*/
	
	@Override
	protected void updateStatusWhenBeginExecution() throws DataAccessException {
		// Message 不需做 
	}
	
	@Override
	protected void updateStatusWhenExecutionSuccess()
			throws DataAccessException {
		// Message 不需做
	}
	
	@Override
	protected void updateStatusWhenExecutionfailed() throws DataAccessException {
		// Message 不需做
	}
	
	protected abstract void executeMessageType()
										throws MessageTypeExecutionException;
	
	// --- getter and setter ---

	public void setSimpleMessageUnitOperation(
			MessageUnitOperation simpleMessageUnitOperation) {
		this.simpleMessageUnitOperation = simpleMessageUnitOperation;
	}

	public void setBusinessTargetActionDao(
			IBusinessTargetActionDao businessTargetActionDao) {
		this.businessTargetActionDao = businessTargetActionDao;
	}
}
